<?php
/*

Version 0.1
SEMF - Web Desiging Framework for Sport Event Managment
Copyright (C) 2006, SEMF Developers

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/
function get_database_info($project_dir)
{
	$project_doc = new DOMDocument();
	$project_xml = $project_dir."/"."project.xml";
	$project_doc->load($project_xml);

	if($project_doc == NULL || $project_doc->documentElement == NULL)
	{
		header("Location: http://" . $_SERVER['HTTP_HOST']
					 . dirname($_SERVER['PHP_SELF'])
					 . "/" . $relative_url); /* Redirect browser */
	}
	
	$the_project = array();
	
	$child_nodes = $project_doc->documentElement->childNodes;
	foreach($child_nodes as $child_node)
	{
		if($child_node->tagName == "database")
		{
			$db_name = $child_node->getAttribute("name");
			$the_project["db_name"] = $db_name;
			
			$server_child_nodes = $child_node->childNodes;
			foreach($server_child_nodes as $server_child_node)
			{
				if($server_child_node->tagName == "password")
				{
					$db_password = getNodeValue($server_child_node);
					$the_project["db_password"] = $db_password;
				}
				elseif($server_child_node->tagName == "server")
				{
					$db_server = getNodeValue($server_child_node);
					$the_project["db_server"] = $db_server;
				}
			}
		}
	}
	
	return $the_project;
}

function getNodeValue($node)
{
	if($node->firstChild)
	{
		return $node->firstChild->nodeValue;
	}
	return "";
}

function createSubmitPage($url, $args, $from_parent)
{
	$form_id = "redirect_form";
	if($from_parent == 1)
	{
		$redirect_form_container_id = "redirectFormContainer";
		echo 
		"<html>
			<head>
			<title>Redirecting to ${url}</title>
			</head>
			<body>
				<h1>
				<div id='${redirect_form_container_id}'>
				<form id='${form_id}' method='post' action='${url}'>";
		
		$arg_keys = array_keys($args);
		foreach($arg_keys as $arg_key)
		{
			$arg_value = $args[$arg_key];
			echo "<input type='hidden' name='${arg_key}' value='${arg_value}'/>";
		}
		echo "
				</form>
				</div>
				<script language='javascript'>
					initPage();
					function initPage()
					{
						var redirect_form_container = 
								document.getElementById('${redirect_form_container_id}');
						var parent_redirect_form_container = 
								parent.document.getElementById('${redirect_form_container_id}');
						if(parent_redirect_form_container)
						{
							parent_redirect_form_container.innerHTML = 
									redirect_form_container.innerHTML;
							var redirect_form = parent.document.getElementById('${form_id}');
							redirect_form.submit();
						}
						
					}
				</script>
			</body>
		</html>";
	}
	else
	{
		echo 
		"<html>
			<head>
			<title>Redirecting to ${url}</title>
			</head>
			<body>
				<form id='${form_id}' method='post' action='${url}'>";
		
		$arg_keys = array_keys($args);
		foreach($arg_keys as $arg_key)
		{
			$arg_value = $args[$arg_key];
			echo "<input type='hidden' name='${arg_key}' value='${arg_value}'/>";
		}
		echo "
				</form>
				<script language='javascript'>
					initPage();
					function initPage()
					{
						var redirect_form = document.getElementById('${form_id}');
						redirect_form.submit();
					}
				</script>
			</body>
		</html>";
	}
}

?>